/*
 * CDeleteLinkAction.java
 *
 * Created on March 3, 2007, 1:02 AM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.actions;

import org.fpdev.core.data.LegacyDB;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.transit.Station;
import org.fpdev.apps.rtemaster.RouteMaster;
import org.fpdev.apps.rtemaster.gui.*;

/**
 *
 * @author demory
 */
public class DeleteLinkAction implements ACAction {

  private BLink link_;
  private Station station_;
  private boolean updateDB_;

  /** Creates a new instance of CDeleteLinkAction */
  public DeleteLinkAction(BLink link, boolean updateDB) {
    link_ = link;
    station_ = null;
    updateDB_ = updateDB;
  }

  public String getName() {
    return "Delete Link";
  }

  public void updateDB(LegacyDB db) {
  }

  public boolean doAction(RouteMaster ac) {
    ac.getEngine().getBaseNet().deleteLink(link_);

    if (link_.isStation()) {
      station_ = link_.getStation();
      station_.removeLink(link_);
      //av.getStationOps().writeStationFile();
    }

    if (updateDB_) {
      ac.getEngine().getDataPackage().getSpatialDataConn().deleteLink(link_);
    }

    return true;
  }

  public boolean undoAction(RouteMaster ac) {
    CreateLinkAction cla = new CreateLinkAction(link_, true);
    boolean success = cla.doAction(ac);

    if (success && station_ != null) {
      station_.addLink(link_);
    }

    return success;
  }
}
